package org.mogu.adapter;


/*
 * Copyright (C) 2011 androd源码工作室
 * 
 * Android实战教程--Android2011QQ
 * 
 * taobao : 
 * mail : androidSource@139.com
 * QQ:    androidSource@139.com
 * 
 */
import java.util.ArrayList;
import java.util.List;

import org.mogu.ContactManagerActivity;
import org.mogu.R;
import org.mogu.control.model.IMUser;
import org.mogu.db.ActiveUserHelper;
import org.mogu.db.DBhelper;
import org.mogu.service.InfoSync;
import org.mogu.staticvalue.Convert;
import org.mogu.tools.HeaderPic;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/*
 * 最近联系的数据适配器
 * @author androidSource@139.com
 * */
public class ContactListAdapter extends BaseAdapter 
		implements View.OnClickListener{
	ActiveUserHelper auh;
    SQLiteDatabase readdb;
    String code;
    
	//所属activity
	private Context mContext = null;
	
	//最近联系的数据数据
	public List<IMUser> recentContactV = new ArrayList<IMUser>();
	
	//加载布局文件的对象
	private LayoutInflater mLayoutInflater = null;	
	
	 private int var=1;
	 
	public void upVar(){
		 var+=1;
	 }
	
	public SQLiteDatabase getReaddb() {
		if (readdb == null || !readdb.isOpen()) {
			readdb = auh.getReadableDatabase();
		}
		return readdb;
	}
 	public void close(){
 		if (readdb != null && readdb.isOpen()) {
			readdb.close();
		}
 	}
 	
	//构造函数
	public ContactListAdapter(Context paramContext,String code){
		 this.code=code;
		 auh=DBhelper.getDBHelper(paramContext);
		 mContext = paramContext;
		 mLayoutInflater = LayoutInflater.from(mContext);
		 
		 try {
//				/*
//				 * raw文件夹下把保存的是不需要系统编译的二进制文件
//				 * 从二进制文件中获取输入流
//				 * */
//				InputStream localInputStream = mContext
//						.getResources().openRawResource(
//								R.raw.qq_recent_list);
//				//从配置文件中获取列的节点list
//				mNodeList = DocumentBuilderFactory.newInstance()
//						.newDocumentBuilder()
//						.parse(localInputStream)
//						.getDocumentElement()
//						.getElementsByTagName("Friend");
//				ReadXMLData(mNodeList);
			 getData();
			 //最近10个联系人
//			 this.notifyDataSetChanged();
			} catch (Exception e) {
				e.printStackTrace();
				
				return;
				// TODO: handle exception
			}
	}
	 
	public void getData(){
		 String[] imUserArr={Convert.uname,code};
		 recentContactV.clear();
		 IMUser imUser;
		 synchronized (InfoSync.class) {
			 Cursor userCursor = getReaddb().rawQuery(
					 "select code,title,content,replyType,maincode,level,father,lastUpdateTime from content where username=? and father=? order by lastUpdateTime desc",
					 imUserArr);
			 userCursor.moveToFirst();
			 int i=0;
			 while(!userCursor.isAfterLast()){
				 if(!"0".equals(userCursor.getString(1))){
					 imUser=new IMUser();
					 imUser.setCode(userCursor.getString(0));
					 imUser.setTitle(userCursor.getString(1));
					 imUser.setContent(userCursor.getString(2));
					 imUser.setReplyType(userCursor.getString(3));
					 imUser.setMaincode(userCursor.getString(4));
					 imUser.setLevel(userCursor.getString(5));
					 imUser.setFather(userCursor.getString(6));
					 imUser.setLastUpdateTime(userCursor.getString(7));
					 recentContactV.add(imUser);
					 i+=1;
					 if(i>=10){
						 break;
					 }
				 }
				
				 userCursor.moveToNext();
			 }
			 userCursor.close();
			 close();
		 }
		 this.notifyDataSetChanged();
	 }
	
	/*
	 * 获得list的列表项总数
	 * */
	public int getCount(){
	    return recentContactV.size();
	}

	/*
	 * 获得list的列表项
	 * */
	public Object getItem(int paramInt){
		 return recentContactV.get(paramInt);
	}

	/*
	 * 获得list的列表项下标
	 * */
	public long getItemId(int paramInt){
	    return paramInt;
	}

	/*
	 * 列表项的视图函数
	 * */
	public View getView(int paramInt, View paramView, ViewGroup paramViewGroup){
		System.out.println("getView");
		
		View localView = paramView;
		//判断当前view视图参数是否为null
	    if (localView == null){
	        //加载一级视图的布局文件
	    	localView = mLayoutInflater.inflate(R.layout.recent_list_item, null);
	    }
	    /*
	     * 相关控件设置
	     * */
	    //头像
	    ImageView headIcon = (ImageView)localView.findViewById(R.id.recent_headIcon);
	    //在线标识
//	    ImageView buddyStatue = (ImageView)localView.findViewById(R.id.recent_buddyStatue);
	    //昵称
	    TextView name = (TextView)localView.findViewById(R.id.recent_name);
	    //心情
	    TextView info = (TextView)localView.findViewById(R.id.recent_info);
	    //编辑按钮
	    ImageView contextbtn = (ImageView)localView.findViewById(R.id.recent_contextbtn);
	    //设置编辑按钮的单击事件监听器
	    contextbtn.setOnClickListener(this);
	    
	    
	    /*
	     * 设置控件内容
	     * */
//    	TreeNode childItem = (TreeNode)recentContactV.get(paramInt);
    	IMUser imUser=(IMUser)getItem(paramInt);
	    name.setText(imUser.getUnicename());
	    info.setText(imUser.getDesc());
	    String[] imUserArr={Convert.uname,imUser.getCode()};
	    String[] imUserArr2={Convert.uname,imUser.getCode(),"0"};
	    int num=0;
	    int contentid=0;
	    if(imUser.getVar()==var){
	    	num=imUser.getCount();
	    }else{
		    synchronized (InfoSync.class) {
		    	Cursor userCursor = getReaddb().rawQuery(
						 "select contentid from contentread where username=? and code=? ",
						 imUserArr);
				 userCursor.moveToFirst();
				 while(!userCursor.isAfterLast()){
					 contentid=userCursor.getInt(0);
					 userCursor.moveToNext();
					 break;
				 }
				 userCursor.close();
				 imUserArr2[2]=String.valueOf(contentid);
				 userCursor = getReaddb().rawQuery(
						 "select count(*) as num from content where username=? and father=? and id>?",
						 imUserArr2);
				 userCursor.moveToFirst();
				 while(!userCursor.isAfterLast()){
					 num=userCursor.getInt(0);
					 userCursor.moveToNext();
					 break;
				 }
				 userCursor.close();
				 imUser.setCount(num);
				 imUser.setHascount(true);
		    }
		    imUser.setVar(var);
		    
	    }
	    if(num>0){
	    	name.append("( "+num+" 条未读)");
	    }
	    	headIcon.setImageResource(HeaderPic.getHeader(imUser.getHeader()));
//	    	buddyStatue.setImageResource(R.drawable.status_offline);
	    
	    //设置视图间距
	    localView.setPadding(0, 0, 0, 0);
	    //获取资源文件对象
	    Drawable localDrawable = mContext.getResources().getDrawable(R.drawable.highlight_item);
	    //设置视图背景
	    localView.setBackgroundDrawable(localDrawable);
	    
	    
	    return localView;
	}
	
	/*
	 * 控件单击事件监听器函数
	 * */
	@Override
	public void onClick(View view){
		Intent mIntent = new Intent(mContext, ContactManagerActivity.class);
		mContext.startActivity(mIntent);
	}
  
}
